package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.itheima.pojo.WebVisit;
import com.itheima.service.WebVisitService;
import com.itheima.utils.ResultUtil;
import com.itheima.vo.WebVisitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户访问日志相关接口
 */
@RestController
@RequestMapping("/webVisit")
@Api(tags = "用户访问日志相关接口")
public class WebVisitController {
    @Autowired
    private WebVisitService webVisitService;


    @PostMapping("/getList")
    @ApiOperation("获取所有用户的访问日志")
    public String getList(@RequestBody WebVisitVO webVisitVO) {
        //设置默认当前页
        if (webVisitVO.getCurrentPage()==null || "".equals(webVisitVO.getCurrentPage())){
            webVisitVO.setCurrentPage(0);
        }
        //设置默认每页条数
        if (webVisitVO.getPageSize()==null || "".equals(webVisitVO.getPageSize())){
            webVisitVO.setPageSize(10);
        }

        try {
            Page<WebVisit> list = webVisitService.getList(webVisitVO);
            Map<String, Object> map = new HashMap();
            map.put("records",list.getResult());
            map.put("total",list.getTotal());
            map.put("size",list.getPageSize());
            map.put("current",list.getPageNum());
            map.put("orders","");
            map.put("optimizeCountSql",true);
            map.put("isSearchCount",true);
            return ResultUtil.result("success", map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(401, "查询失败");
        }
    }
}
